#include "const.h"

int getcpucount() {
	static int cpucount=0;

	if (cpucount)
		return cpucount;

#if defined(_WIN32)
	SYSTEM_INFO si;

	memset(&si, 0, sizeof(si));
	GetSystemInfo(&si);

	cpucount = (int)si.dwNumberOfProcessors;
#elif defined(linux)||defined(__linux)||defined(__linux__)
# ifdef _SC_NPROCESSORS_ONLN
	if ((cpucount = sysconf(_SC_NPROCESSORS_ONLN)) <= 0)
		cpucount = 1;
# else
	// TODO: try reading /proc/ for the cpucount
	cpucount = 1;
# endif
#elif defined(__APPLE__)
	size_t n;
	int mib[2];

	mib[0] = CTL_HW;
	mib[1] = HW_NCPU;

	n = sizeof(cpucount);
	sysctl(mib, sizeof(mib)/sizeof(mib[0]), &cpucount, &n, (void *)0, 0);
#else
	// !!! CODE THIS IN FOR YOUR PLATFORM !!!
	cpucount = 1;
#endif

	return cpucount;
}
